﻿// Decompiled with JetBrains decompiler
// Type: Netick.Unity.NetworkBehaviourRef`1
// Assembly: Netick.Unity, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E128C7FD-39ED-4452-86AC-6EC748457030
// Assembly location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.Unity.dll
// XML documentation location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.Unity.xml

using System;
using System.Runtime.CompilerServices;

#nullable disable
namespace Netick.Unity;

/// <summary>
/// Represents a network-serializable reference to a <see cref="T:Netick.Unity.NetworkBehaviour" /> of type <typeparamref name="T" /> within the networked simulation.
/// This struct stores the object and behaviour ids, allowing retrieval of the referenced behaviour from a <see cref="T:Netick.Unity.NetworkSandbox" />.
/// </summary>
/// <typeparam name="T">The type of <see cref="T:Netick.Unity.NetworkBehaviour" /> being referenced.</typeparam>
[Serializable]
public struct NetworkBehaviourRef<T> : IEquatable<NetworkBehaviourRef<T>> where T : NetworkBehaviour
{
  public readonly int ObjectId;
  public readonly int BehaviourId;

  public NetworkBehaviourRef(NetworkBehaviour beh)
  {
    this.ObjectId = (UnityEngine.Object) beh != (UnityEngine.Object) null ? beh.Object.Id : -1;
    this.BehaviourId = (UnityEngine.Object) beh != (UnityEngine.Object) null ? beh.BehaviourId : -1;
  }

  public bool IsValid => this.ObjectId > 0;

  public bool TryGetBehaviour(NetworkSandbox sandbox, out T behaviour)
  {
    NetworkObject networkObject;
    if (sandbox.TryGetObject(this.ObjectId, out networkObject))
    {
      T behaviourById = networkObject.GetBehaviourById<T>(this.BehaviourId);
      if ((UnityEngine.Object) behaviourById != (UnityEngine.Object) null)
      {
        behaviour = behaviourById;
        return true;
      }
    }
    behaviour = default (T);
    return false;
  }

  public T GetBehaviour<T>(NetworkSandbox sandbox) where T : NetworkBehaviour
  {
    NetworkObject networkObject;
    if (sandbox.TryGetObject(this.ObjectId, out networkObject))
    {
      T behaviourById = networkObject.GetBehaviourById<T>(this.BehaviourId);
      if ((UnityEngine.Object) behaviourById != (UnityEngine.Object) null)
        return behaviourById;
    }
    return default (T);
  }

  public T GetBehaviour(NetworkSandbox sandbox)
  {
    NetworkObject networkObject;
    if (sandbox.TryGetObject(this.ObjectId, out networkObject))
    {
      T behaviourById = networkObject.GetBehaviourById<T>(this.BehaviourId);
      if ((UnityEngine.Object) behaviourById != (UnityEngine.Object) null)
        return behaviourById;
    }
    return default (T);
  }

  [MethodImpl(MethodImplOptions.AggressiveInlining)]
  public override bool Equals(object other)
  {
    return other is NetworkBehaviourRef<T> other1 && this.Equals(other1);
  }

  [MethodImpl(MethodImplOptions.AggressiveInlining)]
  public override int GetHashCode()
  {
    return this.ObjectId.GetHashCode() ^ this.BehaviourId.GetHashCode() << 2;
  }

  [MethodImpl(MethodImplOptions.AggressiveInlining)]
  public bool Equals(NetworkBehaviourRef<T> other)
  {
    return this.ObjectId.Equals(other.ObjectId) && this.BehaviourId.Equals(other.BehaviourId);
  }

  public static bool operator ==(NetworkBehaviourRef<T> a, NetworkBehaviourRef<T> b) => a.Equals(b);

  public static bool operator !=(NetworkBehaviourRef<T> a, NetworkBehaviourRef<T> b)
  {
    return !a.Equals(b);
  }

  public override string ToString()
  {
    return $"Object Id: {this.ObjectId} | Behaviour Id: {this.BehaviourId}";
  }
}
